//
//  CWVisibleView.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/22.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit
import SnapKit

class CWVisibleView: UIView {

    /*
     自定义访客视图控制器
     */
    
    //定义登录后的回调闭包
    var loginCallBack: (()->())?
    
    //懒加载控件
    //fileprivate: 可以在分类中使用属性
    //旋转图片
    fileprivate lazy var cycleImageView: UIImageView = {
        let imgV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imgV
        
    }()
    
    //遮罩图片
    private lazy var coverImageView: UIImageView = {
        
        let imgV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imgV
        
    }()
    
    //房子图片
    private lazy var homeImageView: UIImageView = {
        
        let imgV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return imgV
    }()
    
    //消息文本
    private lazy var massegeLabel: UILabel = {
       
        let label: UILabel = UILabel()
        
        label.text = "关注一些人，回到这里看看有什么惊喜"
        
        label.font = UIFont.systemFont(ofSize: 14)
        
        label.textColor = UIColor.darkGray
        
        label.numberOfLines = 0
        
        label.textAlignment = NSTextAlignment.center
        
        return label
    }()
    
    //注册、登录按钮
    private lazy var registButton: UIButton = {
       
        let button: UIButton = UIButton()
        button.addTarget(self, action: #selector(clickRegistButton), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        let image: UIImage = UIImage(named: "common_button_white_disable")!
        let stretchImage = image.stretchableImage(withLeftCapWidth: 1, topCapHeight: 1)
        button.setBackgroundImage(stretchImage, for: .normal)
        //去掉点击高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    private lazy var loginButton: UIButton = {
        
        let button: UIButton = UIButton()
        button.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        let image: UIImage = UIImage(named: "common_button_white_disable")!
        let stretchImage = image.stretchableImage(withLeftCapWidth: 1, topCapHeight: 1)
        button.setBackgroundImage(stretchImage, for: .normal)
        //去掉点击高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    @objc private func setupUI() {
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        addSubview(cycleImageView)
        
        addSubview(coverImageView)
        
        addSubview(homeImageView)
        
        addSubview(massegeLabel)
        
        addSubview(registButton)
        
        addSubview(loginButton)
        
        cycleImageView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        
        coverImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
        }
        
        homeImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(coverImageView)
            
        }
        
        massegeLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(cycleImageView.snp.bottom)
            
            make.centerX.equalTo(cycleImageView)
            
            make.width.equalTo(225)
            
        }
        
        registButton.snp.makeConstraints { (make) in
            
            make.leading.equalTo(massegeLabel)
            
            make.top.equalTo(massegeLabel.snp.bottom).offset(10)
            
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            
            make.top.equalTo(registButton)
            
            make.size.equalTo(registButton)
            
            make.trailing.equalTo(massegeLabel)
        }
        
        /*
         createAnimation()
         */
        
    }
    
    private func createAnimation() {
        
        let baseAni: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        baseAni.toValue = M_PI * 2
        
        baseAni.repeatCount = MAXFLOAT
        
        baseAni.duration = 10
        
        //完成时不删除动画
        baseAni.isRemovedOnCompletion = false;
        
        cycleImageView.layer.add(baseAni, forKey: nil)
        
    }
    
    //修改访客视图信息的方法
    open func updateVisibleViewInfo(labelText: String?,imageName: String?) {
        
        guard let text = labelText,let image = imageName else {
            //是首页
            createAnimation()
            return
        }
        //不是首页
        homeImageView.image = UIImage(named: image)
        
        massegeLabel.text = text
        
        cycleImageView.isHidden = true;
        
    }
    
    //MARK: 按钮的点击方法
    @objc private func clickRegistButton() {
        
        
        
    }
    
    @objc private func clickLoginButton() {
        
        //点击登录执行闭包
        loginCallBack?()
        
    }
}

extension CWVisibleView {
    
    func layout() {
        
        //手写代码添加autoLayout
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        
        //创建约束
        let cycleCenterXConstrain = NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
        
        let cycleCenterYConstrain = NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
        
        addConstraint(cycleCenterXConstrain)
        
        addConstraint(cycleCenterYConstrain)
        
    }
    
}







